import scrapy

from tencent.items import TencentItem


class TxzpSpider(scrapy.Spider):
    name = 'txzp'
    # allowed_domains = ['www.xxx.com']
    # https: // careers.tencent.com / tencentcareer / api / post / Query?timestamp = 1636297651740 & countryId = & cityId = & bgIds = & productId = & categoryId = & parentCategoryId = & attrId = & keyword = & pageIndex = 2 & pageSize = 10 & language = zh - cn & area = cn
    # https: // careers.tencent.com / search.html?index = 2
    start_urls = [
        f'https://careers.tencent.com/tencentcareer/api/post/Query?timestamp=1636297651740&countryId=&cityId=&bgIds=&productId=&categoryId=&parentCategoryId=&attrId=&keyword=&pageIndex={index}&pageSize=10&language=zh-cn&area=cn'
        for index in range(1, 10)]

    def parse(self, response):
        #item = {}
        item = TencentItem()
        index_json = response.json()["Data"]["Posts"]
        for post in index_json:
            item["LocationName"] = post["LocationName"]  # 地址
            item["ProductName"] = post["ProductName"]  # 公司
            item["LastUpdateTime"] = post["LastUpdateTime"]  # 时间
            item["RecruitPostName"] = post["RecruitPostName"]  # 职位
            item["Responsibility"] = post["Responsibility"].replace("\r", '').replace('\n', '')  # 职位描述
            yield item
